package utils

import (
	"math/rand"
	"strconv"
	"time"

	"github.com/gogo/protobuf/proto"
	"github.com/tendermint/tendermint/my_test/config"
	"github.com/tendermint/tendermint/my_test/utils"
	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

func GenerateTx(dataType tmproto.DataType) ([]byte, error) {
	objectId := rand.Intn(config.MaxObjectId) + 1
	objectIdString := strconv.Itoa(objectId)

	data := &tmproto.FileData{
		Eigenvalue: utils.RandomPoint(),
		ObjectId:   objectIdString,
		OwnerId:    objectIdString,
		Time:       time.Now().Format("2006-01-02 15:04:05"),
	}

	pbData, err := proto.Marshal(data)
	if err != nil {
		return nil, err
	}

	tx := &tmproto.Tx{
		OperationType: tmproto.OperationType_PUT,
		Data:          pbData,
		DataType:      dataType,
	}

	pbTx, err := proto.Marshal(tx)
	if err != nil {
		return nil, err
	}

	return pbTx, nil
}
